package com.sdkx.wear_os.customview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 仪表盘
 * * 注意:因为是在固定开发板用,没怎么考虑适配情况.
 * 1.表盘大小是根据控件布局文件的width决定
 * 2.控件宽度太小可能刻度文字显示不全,可以看情况调整下文字大小.
 */
public class DashboardView extends View {
    private static final String TAG = "DashboardView";
    private Paint arcPaint;
    //圆环的角度
    private int SWEEPANGLE = 360;
    //刻度画笔
    private Paint pointerPaint;
    //红色画笔
    private Paint redPaint;

    private Context mContext;

    //圆环半径
    private int mRadius;
    //圆环的宽度
    int arcW = 20;
    //发光的宽度
    int gleamyArcW = arcW * 3;
    //刻度的长度
    private Paint gleamyArcPaint;

    private Path pointerPath;

    private float currentDegree = 0;
    private ValueAnimator mAnim;
    private List<Integer> angle;
    private int startAngle = -1;
    private int endAngle = -1;


    public DashboardView(Context context) {
        super(context);
        init(context);
    }


    public DashboardView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        // 关闭硬件加速
        setLayerType(LAYER_TYPE_SOFTWARE, null);

        //圆环画笔
        arcPaint = new Paint();
        arcPaint.setStyle(Paint.Style.STROKE);//画线模式
        arcPaint.setStrokeWidth(arcW);//线宽度
        arcPaint.setColor(Color.parseColor("#5076DA"));
        arcPaint.setAntiAlias(true);
        //刻度
        pointerPaint = new Paint();
        pointerPaint.setAntiAlias(true);
        pointerPaint.setStyle(Paint.Style.FILL);
        pointerPaint.setColor(Color.parseColor("#5076DA"));
        pointerPaint.setTextSize(40);
        pointerPaint.setTextAlign(Paint.Align.RIGHT);
        //刻度
        redPaint = new Paint();
        redPaint.setAntiAlias(true);
        redPaint.setColor(Color.parseColor("#E01414"));
        redPaint.setTextSize(40);
        redPaint.setTextAlign(Paint.Align.RIGHT);

        pointerPath = new Path();

        //扇形
        gleamyArcPaint = new Paint();
        gleamyArcPaint.setAntiAlias(true);
        gleamyArcPaint.setColor(Color.parseColor("#F2F9FF"));
        gleamyArcPaint.setStyle(Paint.Style.FILL);
        gleamyArcPaint.setStrokeWidth(gleamyArcW);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mRadius = (int) (getMeasuredWidth() / 2 * 0.8);
//        mRadiusG = mRadius - gleamyArcW / 2;
//        shade_w = (int) (mRadius * 0.4);
        //圆环
        drawArcs(canvas);
        if (angle != null) {
//            drawPointer2(canvas);
            //内部扇形
            drawArc(canvas);
        }
        //指针
        drawPointer(canvas);
        //中心圆
        drawCircle(canvas);
        //文字
        drawText(canvas);
    }

    private void drawPointer(Canvas canvas) {
        canvas.translate(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
        if (startAngle != -1) {
//            canvas.save();
//            canvas.rotate(startAngle - 90);
            pointerPaint.setColor(Color.parseColor("#5076DA"));
            pointerPath.moveTo(rotateXY(mRadius - 50, 0, startAngle - 90).get(0), rotateXY(mRadius - 50, 0, startAngle - 90).get(1));
            pointerPath.lineTo(rotateXY(0, -4, startAngle - 90).get(0), rotateXY(0, -4, startAngle - 90).get(1));
            pointerPath.lineTo(rotateXY(0, 4, startAngle - 90).get(0), rotateXY(0, 4, startAngle - 90).get(1));
            pointerPath.close();
            canvas.drawPath(pointerPath, pointerPaint);
//            canvas.restore();
        }
        if (endAngle != -1) {
//            canvas.save();
//            canvas.translate(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
//            canvas.rotate(endAngle - 90);
            pointerPaint.setColor(Color.parseColor("#5076DA"));
            pointerPath.moveTo(rotateXY(mRadius - 50, 0, endAngle - 90).get(0), rotateXY(mRadius - 50, 0, endAngle - 90).get(1));
            pointerPath.lineTo(rotateXY(0, -4, endAngle - 90).get(0), rotateXY(0, -4, endAngle - 90).get(1));
            pointerPath.lineTo(rotateXY(0, 4, endAngle - 90).get(0), rotateXY(0, 4, endAngle - 90).get(1));
            pointerPath.close();
            canvas.drawPath(pointerPath, pointerPaint);
//            canvas.restore();
        }
    }

    private void drawPointer2(Canvas canvas) {
        for (int i = 0; i < angle.size(); i += 2) {
            if (Objects.equals(angle.get(i), angle.get(i + 1))) {
                return;
            }
            if (angle.get(i) == 0 && angle.get(i + 1) == 360) {
                return;
            }
//            canvas.save();
//            canvas.translate(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
//            canvas.rotate(angle.get(i) - 90);
            pointerPaint.setColor(Color.parseColor("#5076DA"));
            pointerPath.moveTo(rotateXY(mRadius, -2, angle.get(i) - 90).get(0), rotateXY(mRadius, -2, angle.get(i) - 90).get(1));
            pointerPath.lineTo(rotateXY(mRadius, 2, angle.get(i) - 90).get(0), rotateXY(mRadius, 2, angle.get(i) - 90).get(1));
            pointerPath.lineTo(rotateXY(0, 2, angle.get(i) - 90).get(0), rotateXY(0, 2, angle.get(i) - 90).get(1));
            pointerPath.lineTo(rotateXY(0, -2, angle.get(i) - 90).get(0), rotateXY(0, -2, angle.get(i) - 90).get(1));
//            pointerPath.moveTo(mRadius, -2);
//            pointerPath.lineTo(mRadius, 2);
//            pointerPath.lineTo(0, 2);
//            pointerPath.lineTo(0, -2);
            pointerPath.close();
            canvas.drawPath(pointerPath, pointerPaint);
//            canvas.restore();
//            canvas.save();
//            canvas.translate(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
//            canvas.rotate(angle.get(i + 1) - 90);
            pointerPaint.setColor(Color.parseColor("#5076DA"));
            pointerPath.moveTo(rotateXY(mRadius, -2, angle.get(i + 1) - 90).get(0), rotateXY(mRadius, -2, angle.get(i + 1) - 90).get(1));
            pointerPath.lineTo(rotateXY(mRadius, 2, angle.get(i + 1) - 90).get(0), rotateXY(mRadius, 2, angle.get(i + 1) - 90).get(1));
            pointerPath.lineTo(rotateXY(0, 2, angle.get(i + 1) - 90).get(0), rotateXY(0, 2, angle.get(i + 1) - 90).get(1));
            pointerPath.lineTo(rotateXY(0, -2, angle.get(i + 1) - 90).get(0), rotateXY(0, -2, angle.get(i + 1) - 90).get(1));

//            pointerPath.moveTo(mRadius, -2);
//            pointerPath.lineTo(mRadius, 2);
//            pointerPath.lineTo(0, 2);
//            pointerPath.lineTo(0, -2);
            pointerPath.close();
            canvas.drawPath(pointerPath, pointerPaint);
//            canvas.restore();
        }
    }

    private void drawArcs(Canvas canvas) {
        RectF rectF = new RectF(getMeasuredWidth() / 2 - mRadius, getMeasuredHeight() / 2 - mRadius,
                getMeasuredWidth() / 2 + mRadius, getMeasuredHeight() / 2 + mRadius);
        canvas.drawArc(rectF, 0, SWEEPANGLE, false, arcPaint);
    }

    private void drawArc(Canvas canvas) {
        for (int i = 0; i < angle.size(); i += 2) {
//            canvas.save();
//            RectF oval2 = new RectF(getMeasuredWidth() / 2 - mRadius + 10, getMeasuredHeight() / 2 - mRadius + 10, getMeasuredWidth() / 2 + mRadius - 10, getMeasuredHeight() / 2 + mRadius - 10);// 设置个新的长方形，扫描测量
            RectF oval2 = new RectF(70, 70, mRadius * 2 + 50, mRadius * 2 + 50);// 设置个新的长方形，扫描测量
//            RectF oval2 = new RectF(-mRadius + 10, -mRadius + 10, mRadius - 10, mRadius - 10);// 设置个新的长方形，扫描测量
            canvas.drawArc(oval2, angle.get(i) - 90, angle.get(i + 1) - angle.get(i), true, gleamyArcPaint);
//            canvas.restore();
        }
    }

    private void drawCircle(Canvas canvas) {
//        canvas.save();
//        canvas.translate(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
        canvas.drawCircle(0, 0, 10, pointerPaint);// 小圆
    }

    private void drawText(Canvas canvas) {
//        canvas.save();
        canvas.drawText("0", 10, -mRadius + arcW * 2, pointerPaint);// 小圆
        canvas.drawText("北", 20, -mRadius + arcW * 2 + 40, redPaint);// 小圆
        canvas.drawText("90", mRadius - arcW, 10, pointerPaint);// 小圆
        canvas.drawText("180", 30, mRadius - arcW, pointerPaint);// 小圆
        canvas.drawText("270", -mRadius + arcW * 4, 10, pointerPaint);// 小圆
    }

    /**
     * 旋转之后的坐标
     */
    public List<Float> rotateXY(double x, double y, double angle) {
        List<Float> xy = new ArrayList<Float>();
        //旋转弧度
        double arc = Math.PI / 180 * angle;
        double newX = x * Math.cos(arc) - y * Math.sin(arc);
        double newY = x * Math.sin(arc) + y * Math.cos(arc);
        xy.add(Float.parseFloat(String.valueOf(newX)));
        xy.add(Float.parseFloat(String.valueOf(newY)));
        return xy;
    }

    /**
     * 外部刷新数据
     */
    public void updateData(List<Integer> angle, int startAngle, int endAngle) {
        this.angle = angle;
        this.startAngle = startAngle;
        this.endAngle = endAngle;
        invalidate();
    }

    /**
     * 外部更新刻度.
     *
     * @param speedssss .
     */
    public void udDataSpeed(int speedssss) {
        float a = SWEEPANGLE / 180f;
        if (speedssss < 0) throw new IllegalArgumentException("----speed不能小于0----");
//        speed = String.valueOf(speedssss);
        //Log.i(TAG, "udDataSpeed: \ncurrentDegree:"+currentDegree+"\na:"+a+"\nspeedssss:"+speedssss);
        startAnimation(currentDegree, (float) speedssss * a);
    }

    //指针+阴影偏移动画.
    private void startAnimation(float start, float end) {
        if (mAnim != null) {
            if (mAnim.isRunning() || mAnim.isStarted()) {
                mAnim.cancel();
                mAnim.removeAllUpdateListeners();
            }
            boolean running = mAnim.isRunning();
            boolean started = mAnim.isStarted();
            Log.i(TAG, "startAnimation: running:" + running + "--started" + started);
        }
        mAnim = ValueAnimator.ofFloat(start, end);
        //anim.setRepeatCount(ValueAnimator.INFINITE);//设置无限重复
        //anim.setRepeatMode(ValueAnimator.REVERSE);//设置重复模式
        mAnim.setDuration(500);
        mAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float value = (float) mAnim.getAnimatedValue();
                //Log.i(TAG, "onAnimationUpdate: " + value);
                currentDegree = value;
                invalidate();
            }
        });
        mAnim.start();
    }

    /**
     * 退出动画.
     */
    public void closeAnimation() {
        if (mAnim != null) {
            mAnim.cancel();
            mAnim.removeAllUpdateListeners();
        }
    }

}

